﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NS_QuangCaoTiepThiBUS
{
    public class NoiDungBUS
    {
        public void Them(NoiDung ndDTO)
        {
            QCTTDataContext storeDB = new QCTTDataContext();

            storeDB.NoiDungs.InsertOnSubmit(ndDTO);

            storeDB.SubmitChanges();
        }

        public void Sua(NoiDung ndDTO)
        {
            QCTTDataContext storeDB = new QCTTDataContext();

            var query = storeDB.NoiDungs.Where(q => q.MaNoiDung == ndDTO.MaNoiDung).First();
            query.NoiDungTinTucChung = ndDTO.NoiDungTinTucChung;
            query.NgayBatDauTinTucChung = ndDTO.NgayBatDauTinTucChung;
            query.NgayKetThucTinTucChung = ndDTO.NgayKetThucTinTucChung;
            query.NoiDungTinTucNoiBat = ndDTO.NoiDungTinTucNoiBat;
            query.NgayBatDauTinTucNoiBat = ndDTO.NgayBatDauTinTucNoiBat;
            query.NgayKetThucTinTucNoiBat = ndDTO.NgayKetThucTinTucNoiBat;
            query.TenFileHinhAnhNoiBo = ndDTO.TenFileHinhAnhNoiBo;
            query.NgayBatDauHinhAnhNoiBo = ndDTO.NgayBatDauHinhAnhNoiBo;
            query.NgayKetThucHinhAnhNoiBo = ndDTO.NgayKetThucHinhAnhNoiBo;
            query.TenFileHinhAnhKhachHang = ndDTO.TenFileHinhAnhKhachHang;
            query.NgayBatDauHinhAnhKhachHang = ndDTO.NgayBatDauHinhAnhKhachHang;
            query.NgayKetThucHinhAnhKhachHang = ndDTO.NgayKetThucHinhAnhKhachHang;
            query.TenFileVideoNoiBo = ndDTO.TenFileVideoNoiBo;
            query.NgayBatDauVideoNoiBo = ndDTO.NgayBatDauVideoNoiBo;
            query.NgayKetThucVideoNoiBo = ndDTO.NgayKetThucVideoNoiBo;
            query.ChoPhepPhatLienTuc = ndDTO.ChoPhepPhatLienTuc;
            query.MaNguoiSoanThao = ndDTO.MaNguoiSoanThao;
            query.NgaySoanThao = DateTime.Now;
            query.MaNguoiKiemSoat = ndDTO.MaNguoiKiemSoat;
            query.NgayKiemSoat = ndDTO.NgayKiemSoat;

            storeDB.SubmitChanges();
        }

        public void Xoa(NoiDung ndDTO)
        {
            QCTTDataContext storeDB = new QCTTDataContext();

            var query = storeDB.NoiDungs.Where(q => q.MaNoiDung == ndDTO.MaNoiDung).First();
            storeDB.NoiDungs.DeleteOnSubmit(query);
            storeDB.SubmitChanges();
        }

        public IList<NoiDung> LayDanhSachNoiDungChuaDuyet()
        {
            QCTTDataContext storeDB = new QCTTDataContext();

            var query = storeDB.NoiDungs.Where(q => q.NgayKiemSoat == null).ToList();

            return query;
        }

        public IList<NoiDung> LayDanhSachNoiDungChuaDuyetTheoNgay(DateTime ngayTu, DateTime ngayDen)
        {
            QCTTDataContext storeDB = new QCTTDataContext();
            
            if (ngayTu == null && ngayDen == null)
            {
                var query = storeDB.NoiDungs.Where(q => q.NgayKiemSoat == null).ToList();

                return query;
            }

            if (ngayTu != null && ngayDen != null)
            {
                var query = storeDB.NoiDungs.Where(q => q.NgayKiemSoat == null && q.NgaySoanThao.Value.Date >= ngayTu && q.NgaySoanThao.Value <= ngayDen).ToList();

                return query;
            }

            return null;
        }

        public void Duyet(NoiDung ndDTO)
        {
            QCTTDataContext storeDB = new QCTTDataContext();

            var query = storeDB.NoiDungs.Where(q => q.MaNoiDung == ndDTO.MaNoiDung).First();

            if (query.NgayKiemSoat == null)
            {
                query.NgayKiemSoat = DateTime.Now;
            }

            storeDB.SubmitChanges();
        }

        public IList<NoiDung> LayDanhSachHinhAnhNoiBo()
        {
            QCTTDataContext storeDB = new QCTTDataContext();

            var query = storeDB.NoiDungs.Where(q => q.NgayKiemSoat != null && q.TenFileHinhAnhNoiBo != string.Empty && (q.NgayKetThucHinhAnhNoiBo == null || (q.NgayBatDauHinhAnhNoiBo.Value.Date <=  DateTime.Now.Date && q.NgayKetThucHinhAnhNoiBo.Value.Date >= DateTime.Now.Date))).ToList();

            return query;
        }

        public IList<NoiDung> LayDanhSachHinhAnhKhachHang()
        {
            QCTTDataContext storeDB = new QCTTDataContext();

            var query = storeDB.NoiDungs.Where(q => q.NgayKiemSoat != null && q.TenFileHinhAnhKhachHang != string.Empty && (q.NgayKetThucHinhAnhKhachHang == null || (q.NgayBatDauHinhAnhKhachHang.Value.Date <= DateTime.Now.Date && q.NgayKetThucHinhAnhKhachHang.Value.Date >= DateTime.Now.Date))).ToList();

            return query;
        }

        public IList<NoiDung> LayDanhSachVideo()
        {
            QCTTDataContext storeDB = new QCTTDataContext();

            var query = storeDB.NoiDungs.Where(q => q.NgayKiemSoat != null && q.TenFileVideoNoiBo != string.Empty && (q.NgayKetThucVideoNoiBo == null || (q.NgayBatDauVideoNoiBo.Value.Date <= DateTime.Now.Date && q.NgayKetThucVideoNoiBo.Value.Date >= DateTime.Now.Date))).ToList();

            return query;
        }

        public IList<NoiDung> LayDanhSachNoiDungTinTucChung()
        {
            QCTTDataContext storeDB = new QCTTDataContext();

            var query = storeDB.NoiDungs.Where(q => q.NgayKiemSoat != null && q.NoiDungTinTucChung != string.Empty && (q.NgayKetThucTinTucChung == null || (q.NgayBatDauTinTucChung.Value.Date <= DateTime.Now.Date && q.NgayKetThucTinTucChung.Value.Date >= DateTime.Now.Date))).ToList();

            return query;
        }

        public IList<NoiDung> LayDanhSachNoiDungTinTucNoiBat()
        {
            QCTTDataContext storeDB = new QCTTDataContext();

            var query = storeDB.NoiDungs.Where(q => q.NgayKiemSoat != null && q.NoiDungTinTucNoiBat != string.Empty && (q.NgayKetThucTinTucNoiBat == null || (q.NgayBatDauTinTucNoiBat.Value.Date <= DateTime.Now.Date && q.NgayKetThucTinTucNoiBat.Value.Date >= DateTime.Now.Date))).ToList();

            return query;
        }
    }
}
